package com.tfy.liquor.controller;

import com.tfy.liquor.bean.Result;
import com.tfy.liquor.bean.VendorInfo;
import com.tfy.liquor.service.VendorInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/vendorInfo")
public class VendorInfoController {
    @Autowired
    private VendorInfoService vendorInfoService;

    // 添加供应商信息
    @RequestMapping("/addVendorInfo")
    public Result addVendorInfo(VendorInfo vendorInfo) {
        if (null == vendorInfo) {
            return new Result().bad_CodeIsNull();
        }
        if (vendorInfoService.addVendorInfo(vendorInfo)) {
            return new Result().ok("添加成功！");
        }
        return new Result().error("添加失败！");
    }

    // 根据条件查询供应商信息
    @RequestMapping("/getAllVendorInfo")
    public Result getAllVendorInfo(Integer pageNum, Integer pageSize, VendorInfo vendorInfo) {
        if (null == pageNum || null == pageSize || null == vendorInfo) {
            // 返回参数错误
            return new Result().bad_CodeIsNull();
        }
        if ("".equals(vendorInfo.getVendorName())) {
            vendorInfo.setVendorName(null);
        }
        if ("".equals(vendorInfo.getMobile())) {
            vendorInfo.setMobile(null);
        }
        return new Result(vendorInfoService.getAllVendorInfo(pageNum, pageSize, vendorInfo)).ok();
    }

    // 添加产品时获取正常的供应商信息
    @RequestMapping("/getVendorInfo")
    public Result getVendorInfo() {
        return new Result(vendorInfoService.getVendorInfo()).ok();
    }

    // 修改供应商信息
    @RequestMapping("/editVendorInfo")
    public Result editVendorInfo(VendorInfo vendorInfo) {
        if (null == vendorInfo) {
            return new Result().bad_CodeIsNull();
        }
        if (vendorInfoService.editVendorInfo(vendorInfo)) {
            return new Result().ok("修改成功！");
        }
        return new Result().error("修改失败！");
    }

    // 修改供应商状态
    @RequestMapping("/editVendorInfoStatus")
    public Result editVendorInfoStatus(VendorInfo vendorInfo) {
        if (null == vendorInfo || null == vendorInfo.getId() || null == vendorInfo.getStatus()) {
            return new Result().bad_CodeIsNull();
        }
        if (vendorInfoService.editVendorInfoStatus(vendorInfo)) {
            return new Result().ok("修改成功！");
        }
        return new Result().error("修改失败！");
    }
}
